//
//  CompetionTopView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/29.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CompetionTopView.h"

@implementation CompetionTopView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    
    self.backgroundColor = COLOR_HEX(0xffc91f);
//    self.alpha = 0.1;
    UIButton *liveBtn = [UIButton new];
    [liveBtn setBackgroundColor:[UIColor whiteColor]];
    liveBtn.tag = 1;
    liveBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [liveBtn setTitle:@"赛事通知" forState:UIControlStateNormal];
    [liveBtn setTitle:@"·  赛事通知  ·" forState:UIControlStateSelected];
    [liveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [liveBtn setTitleColor:COLOR_HEX(0xffc91f) forState:UIControlStateSelected];
    liveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [liveBtn addTarget:self action:@selector(tabBtnSwitch:) forControlEvents:UIControlEventTouchUpInside];
    liveBtn.selected = YES;
    _type = 1;
    [self addSubview:liveBtn];
    self.liveButton = liveBtn;
    
    UIButton *inteBtn = [UIButton new];
    inteBtn.tag = 2;
    [inteBtn setTitle:@"赛事回顾" forState:UIControlStateNormal];
    [inteBtn setTitle:@"·  赛事回顾  ·" forState:UIControlStateSelected];
    inteBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [inteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [inteBtn setTitleColor:COLOR_HEX(0xffc91f) forState:UIControlStateSelected];
    inteBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [inteBtn addTarget:self action:@selector(tabBtnSwitch:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:inteBtn];
    self.inteButton = inteBtn;
    [liveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
        make.right.equalTo(inteBtn.mas_left).offset(0);
    }];
    
    [inteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self);
        make.width.mas_equalTo(liveBtn.mas_width);
    }];
    liveBtn.layer.cornerRadius = 15;
    liveBtn.layer.masksToBounds = YES;
    inteBtn.layer.cornerRadius = 15;
    inteBtn.layer.masksToBounds = YES;
    
}
- (void)tabBtnSwitch:(UIButton *)btn {
    if (btn.selected) {
        return;
    }
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIButton class]]) {
            ((UIButton *)obj).selected = NO;
            ((UIButton *)obj).titleLabel.font = [UIFont systemFontOfSize:15];
            ((UIButton *)obj).backgroundColor = [UIColor clearColor];
        }
    }];
    
    btn.selected = YES;
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    btn.backgroundColor = [UIColor whiteColor];
    _type = btn.tag;
    [UIView animateWithDuration:0.7 animations:^{
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(3);
            make.bottom.mas_equalTo(self);
            make.centerX.equalTo(btn.mas_centerX);
        }];
        
    }];
    
    if ([self.delegate respondsToSelector:@selector(selectedGroup:)]) {
        [self.delegate selectedGroup:btn.tag];
    }
}
- (void)switchToTab:(NSInteger)tab {
    if (tab == 1) {
        [self.liveButton sendAction:@selector(tabBtnSwitch:) to:self forEvent:[UIEvent new]];
    }else {
        [self.inteButton sendAction:@selector(tabBtnSwitch:) to:self forEvent:[UIEvent new]];
    }}


@end
